package com.pen.changgou.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pen.changgou.common.core.utils.R;
import com.pen.changgou.goods.entity.*;
import com.pen.changgou.goods.mapper.BrandMapper;
import com.pen.changgou.goods.mapper.CategoryMapper;
import com.pen.changgou.goods.mapper.SkuMapper;
import com.pen.changgou.goods.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.pen.changgou.goods.mapper.SpuMapper;
import com.pen.changgou.goods.service.SpuService;
import org.springframework.util.Assert;

@Slf4j
@Service("spuService")
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {
    @Autowired
    SkuService skuService;

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;

    public R audit(Long id) {
        Spu spu = baseMapper.selectById(id);
        Assert.isNull(spu, "商品不存在");
        //审核通过
        spu.setStatus(GoodsConstants.GOODS_STATUS_UP);
        //商家
        spu.setIsMarketable(GoodsConstants.GOODS_MARKETABLE_AUDIT);
        baseMapper.updateById(spu);
        return R.ok();
    }

    @Override
    public R up(Long id) {
        Spu spu = baseMapper.selectById(id);
        Assert.isNull(spu, "商品不存在");
        spu.setIsMarketable(GoodsConstants.GOODS_MARKETABLE_UP);
        return R.ok(baseMapper.updateById(spu));
    }

    @Override
    public R down(Long id) {
        Spu spu = baseMapper.selectById(id);
        Assert.isNull(spu, "商品不存在");
        spu.setIsMarketable(GoodsConstants.GOODS_MARKETABLE_DOWN);
        return R.ok(baseMapper.updateById(spu));
    }

    @Override
    public R upBatch(Long[] ids) {
        QueryWrapper<Spu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", Arrays.array(ids));
        Spu spu = new Spu();
        spu.setIsMarketable(GoodsConstants.GOODS_MARKETABLE_UP);
        baseMapper.update(spu, queryWrapper);


        return R.ok();
    }

    @Override
    public R downBatch(Long[] ids) {
        QueryWrapper<Spu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", Arrays.array(ids));
        Spu spu = new Spu();
        spu.setIsMarketable(GoodsConstants.GOODS_MARKETABLE_DOWN);
        baseMapper.update(spu, queryWrapper);
        return R.ok();
    }

    @Override
    public R saveGoods(Goods goods) {
        log.info("save goods--start");
        Spu spu = goods.getSpu();
        baseMapper.insert(spu);
        List<Sku> skuList = goods.getSkuList();
        Brand brand = brandMapper.selectById(spu.getBrandId());
        Category category3 = categoryMapper.selectById(spu.getCategory3Id());
        String name = spu.getName();
        skuList.forEach(x -> {
            String spec = x.getSpec();
            if (StringUtils.isEmpty(spec)) {
                spec = "{}";
            }
            Map<String, String> map = JSON.parseObject(spec, Map.class);

            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> specEntry : map.entrySet()) {
                sb.append(" ").append(specEntry.getKey());

            }
            x.setName(name + sb.toString());
            x.setSpuId(spu.getId());
            x.setCategoryId(category3.getId());
            x.setCategoryName(category3.getName());
            x.setBrandName(brand.getName());

        });

        skuService.saveBatch(skuList);
        log.info("save goods--end");
        return R.ok();
    }

    @Override
    public R<Goods> findGoodsById(Long id) {
        log.info("商品查找-start");
        Spu spu = baseMapper.selectById(id);
        List<Sku> skuList = skuService.list(Wrappers.<Sku>lambdaQuery().eq(Sku::getSpuId, id));
        Goods goods = new Goods();
        goods.setSpu(spu);
        goods.setSkuList(skuList);

        return R.ok(goods);
    }
}